tidyverse 與 gapminderlibrary(tidyverse)
library(gapminder)ggplot() + geom_point() 繪製散佈圖gapminder_2007 <- gapminder %>%
filter(year == 2007)
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp)) +
geom_point()aes() 中加入 color =ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point()ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_x_log10()ggplot() + geom_line() 繪製線圖gapminder_tw <- gapminder %>%
filter(country == "Taiwan")
ggplot(gapminder_tw, aes(x = year, y = lifeExp)) +
geom_line()gapminder_na <- gapminder %>%
filter(country %in% c("China", "Hong Kong, China", "Japan", "Korea, Rep.", "Taiwan"))
ggplot(gapminder_na, aes(x = year, y = lifeExp, color = country)) +
geom_line()ggplot() + geom_histogram() 繪製直方圖ggplot(gapminder_2007, aes(x = lifeExp)) +
geom_histogram(bins = 40)ggplot() + geom_boxplot() 繪製盒鬚圖ggplot(gapminder_2007, aes(x = continent, y = lifeExp)) +
geom_boxplot()facet_wrap()ggplot(gapminder_2007, aes(x = lifeExp, fill = continent)) +
geom_histogram(bins = 20) +
facet_wrap(~continent, nrow = 2)ggplot() + geom_bar(stat = "identity")gapminder_2007_na <- gapminder_2007 %>%
filter(country %in% c("China", "Hong Kong, China", "Japan", "Korea, Rep.", "Taiwan"))
ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")+ coord_flip()ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")+
coord_flip()gridExtra 套件來幫忙grid.arrange() 函數install.packages("gridExtra")
library(gridExtra)
gg1 <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")
gg2 <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")+
coord_flip()
grid.arrange(gg1, gg2, nrow = 2)ggplotly() 加入互動性plotly 套件的 ggplotly() 函數install.packages("plotly")
library(plotly)
static_gg <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_x_log10()
ggplotly(static_gg)